package com.sea.product.service.impl;


import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.NewProductStatusEnum;
import com.sea.product.entity.NewProduct;
import com.sea.product.mapper.NewProductMapper;
import com.sea.product.service.NewProductRecordService;
import com.sea.product.service.NewProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 陈鸿飞
 */
@Service
public class NewProductServiceImpl implements NewProductService {

    @Autowired
    private NewProductMapper newProductMapper;
    @Autowired
    private NewProductRecordService newProductRecordService;

    @Override
    public int save(NewProduct newProduct) {
        int res = newProductMapper.insert(newProduct);
        saveAllStatus(newProduct);
        return res;
    }

    @Override
    public List<NewProduct> findByCnd(NewProduct newProduct) {
        return newProductMapper.findByCnd(newProduct);
    }

    @Override
    public int usave(NewProduct newProduct) {
        return newProductMapper.update(newProduct);
    }

    @Override
    public List<Map<String, Object>> findNewProductNumber() {
        return newProductMapper.findNewProductNumber();
    }

    @Override
    public void saveAllStatus(NewProduct product) {
        //遍历更新所有数据
        NewProductStatusEnum[] values = NewProductStatusEnum.values();
        for(NewProductStatusEnum v:values){
            int status = v.getCode();
            if(status==21|| status==31 || status==32){
                continue;
            }else{
                switch (status) {
                    case 1:
                        newProductRecordService.save(product.getId(), status, "", product.getProductTime());
                        break;
                    case 2:
                        newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),15),"yyyy-MM-dd"));
                        break;
                    case 3:
                        newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),23),"yyyy-MM-dd"));
                        break;
                    case 4:
                        newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),26),"yyyy-MM-dd"));
                        break;
                    case 5:
                        newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),29),"yyyy-MM-dd"));
                        break;
                    case 6:
                        String country = product.getProductCountry();
                        if(country.equals("UK")|| country.equals("DE")|| country.equals("FR")|| country.equals("IT")|| country.equals("ES")){
                            newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),71),"yyyy-MM-dd"));
                        }else{
                            newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),59),"yyyy-MM-dd"));
                        }
                        break;
                    case 7:
                         country = product.getProductCountry();
                        if(country.equals("UK")|| country.equals("DE")|| country.equals("FR")|| country.equals("IT")|| country.equals("ES")){
                            newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),76),"yyyy-MM-dd"));
                        }else{
                            newProductRecordService.save(product.getId(), status, "",DateUtils.dateToString( DateUtils.addDay(DateUtils.stringToDate(product.getProductTime(),"yyyy-MM-dd"),64),"yyyy-MM-dd"));
                        }
                        break;
                }
            }
        }
    }

    @Override
    public List<NewProduct> export(NewProduct newProduct) {
        return newProductMapper.export(newProduct);
    }
}
